package com.dmgis.qxfw.dataservice.businessservice.yydl.service.Impl;

import com.dmgis.qxfw.dataservice.businessservice.api.domain.DbConfig;
import com.dmgis.qxfw.dataservice.businessservice.api.domain.RequestDataBySelect;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.msgmediumsmallscale;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.msgmediumsmallscale;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.max_powerload;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.max_powerload_fcst;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.power_product;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.DayLoad;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.MsgMedium;
import com.dmgis.qxfw.dataservice.businessservice.common.util.JsonUtil;
import com.dmgis.qxfw.dataservice.businessservice.common.util.StringUtil;
import com.dmgis.qxfw.dataservice.businessservice.controller.ProcessRequestController;
import com.dmgis.qxfw.dataservice.businessservice.yydl.service.DayPowerSerrviceI;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class DayPowerServiceImpl implements DayPowerSerrviceI {

	ProcessRequestController pc = new ProcessRequestController();

	@Override
	public List<DayLoad> dayPowerLoad(String powerType, String startTime) {
		List<max_powerload_fcst> listData = new ArrayList<max_powerload_fcst>();
		List<DayLoad> resultList = new ArrayList<DayLoad>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add(
				"(cast(datechar as timestamp)+ cast((cast(substring(timechar,0,2) as integer)+ntimes)||'hour' as INTERVAL)) as datechar");
		if (powerType.contains("日最大负荷")) {
			columns.add("daymaxload");
		}
		if (powerType.contains("日最小负荷")) {
			columns.add("dayminload");
		}
		if (powerType.contains("日负荷")) {
			columns.add("dayload");
		}
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		if (startTime != null) {
			strWhere.append(String.format("and datechar='%s'", startTime));
		}
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		selectRequest.setSort(new String[]{"datechar asc"});
		try {
			selectParam.put("method", "max_powerload_fcst");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<max_powerload_fcst>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (listData != null && listData.size() > 0) {
			for (max_powerload_fcst fcst : listData) {
				DayLoad powerload = new DayLoad();
				powerload.setDate(fcst.getDatechar());
				if (powerType.contains("日最大负荷")) {
					powerload.setLoad(fcst.getDaymaxload().floatValue());
				}
				if (powerType.contains("日最小负荷")) {
					powerload.setLoad(fcst.getDayminload().floatValue());
				}
				if (powerType.contains("日负荷")) {
					powerload.setLoad(fcst.getDayload().floatValue());
				}

				resultList.add(powerload);
			}
		}
		return resultList;
	}

	@Override
	public Date initDayPowerLoadTime() {
		// DbContextHolder.setDbType("qx");
		// return dayPowerMapper.initDayPowerLoadTime();
		Date date = new Date();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		String[] columns = new String[] { "to_char(datechar, 'yyyy-MM-dd HH24:MI:SS') as datechar" };
		selectRequest.setColumns(columns);
		selectRequest.setSort(new String[] { "datechar desc" });
		selectRequest.setLimit(1);
		try {
			selectParam.put("method", "max_powerload_fcst");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			date = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<max_powerload_fcst>>() {
			}).get(0).getDatechar();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return date;
	}

	@Override
	public Date initDayPowerLoadTime2() {
		// DbContextHolder.setDbType("zy");
		// return dayPowerMapper.initDayPowerLoadTime2();
		Date date = new Date();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		String[] columns = new String[] { "to_char(forecasttime, 'yyyy-MM-dd HH24:MI:SS') as forecasttime" };
		selectRequest.setColumns(columns);
		selectRequest.setSort(new String[] { "forecasttime desc" });
		selectRequest.setLimit(1);
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		strWhere.append(String.format("and producttype='%s'", "日电力负荷预报"));
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		try {
			selectParam.put("method", "power_product");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			date = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<power_product>>() {
			}).get(0).getForecasttime();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return date;
	}

	@Override
	public List<DayLoad> getMaxPower(String dateTime, String dateTime2) {
		// DbContextHolder.setDbType("qx");
		// return dayPowerMapper.getMaxPower(dateTime, dateTime2);
		List<max_powerload_fcst> listData = new ArrayList<max_powerload_fcst>();
		List<DayLoad> resultList = new ArrayList<DayLoad>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("to_char(datechar, 'yyyy-MM-dd HH24:MI:SS') as datechar");
		columns.add("dayload");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		selectRequest.setSort(new String[] { "datechar asc" });
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		if (dateTime != null) {
			strWhere.append(String.format("and datechar>='%s'", dateTime));
		}
		if (dateTime2 != null) {
			strWhere.append(String.format("and datechar<='%s'", dateTime2));
		}
		strWhere.append(String.format("and ntimes='%s'", "24"));
		strWhere.append(String.format("and timechar='%s'", "20"));
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		try {
			selectParam.put("method", "max_powerload_fcst");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<max_powerload_fcst>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (listData != null && listData.size() > 0) {
			for (max_powerload_fcst fcst : listData) {
				DayLoad powerload = new DayLoad();
				powerload.setDate(fcst.getDatechar());
				powerload.setLoad(fcst.getDayload().floatValue());
				resultList.add(powerload);
			}
		}
		return resultList;
	}

	@Override
	public List<MsgMedium> getMsgMedium(String dateTime, String dateTime2, boolean b) {
		// DbContextHolder.setDbType("qx");
		// return dayPowerMapper.getMsgMedium(dateTime, dateTime2, b);
		List<msgmediumsmallscale> listData = new ArrayList<msgmediumsmallscale>();
		List<msgmediumsmallscale> listData1 = new ArrayList<msgmediumsmallscale>();
		List<MsgMedium> resultList = new ArrayList<MsgMedium>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("to_char(observTime, 'yyyy-MM-dd 00:00:00') as observTime");
		columns.add("max(cast(replace(cast(DryBulbTemp AS VARCHAR),'-65535','0') as numeric(18,1))) as drybulbtemp");
		columns.add("min(cast(replace(cast(DryBulbTemp AS VARCHAR),'-65535','0') as numeric(18,1))) as mintemp");
		columns.add("max(cast(replace(cast(RelHumidity AS VARCHAR),'-65535','0') as numeric(18,0))) as relhumidity");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		selectRequest.setSort(new String[] { "observTime asc" });
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		if (dateTime != null) {
			strWhere.append(String.format(" and observTime>='%s'", dateTime));
		}
		if (dateTime2 != null) {
			strWhere.append(String.format(" and observTime<='%s'", dateTime2));
		}
		strWhere.append(String.format(" and stationid='%s'", DbConfig.STATIONID));
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());

		if (b) {
			try {
				selectParam.put("method", "msgmediumsmallscale");
				selectParam.put("sqlType", "select");
				selectParam.put("param", JsonUtil.object2Json(selectRequest));
				String message = pc.processRequest(selectParam);
				listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<msgmediumsmallscale>>() {
				});
			} catch (Exception e) {
				// TODO: handle exception
			}
			if (listData != null && listData.size() > 0) {
				for (msgmediumsmallscale msg : listData) {
					MsgMedium smallscale = new MsgMedium();
					smallscale.setObservTime(msg.getObservtime());
					smallscale.setDryBulbTemp(msg.getDrybulbtemp().toString());
					smallscale.setMinTemp(msg.getMintemp().toString());
					smallscale.setRelHumidity(msg.getRelhumidity().toString());
					resultList.add(smallscale);
				}
			}
		} else {
			try {
				selectParam.put("method", "msgmediumsmallscale");
				selectParam.put("sqlType", "select");
				selectParam.put("param", JsonUtil.object2Json(selectRequest));
				String message = pc.processRequest(selectParam);
				listData1 = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<msgmediumsmallscale>>() {
				});
			} catch (Exception e) {
				// TODO: handle exception
			}
			if (listData1 != null && listData1.size() > 0) {
				for (msgmediumsmallscale msg : listData1) {
					MsgMedium smallscale = new MsgMedium();
					smallscale.setObservTime(msg.getObservtime());
					smallscale.setDryBulbTemp(msg.getDrybulbtemp().toString());
					smallscale.setMinTemp(msg.getMintemp().toString());
					smallscale.setRelHumidity(msg.getRelhumidity().toString());
					resultList.add(smallscale);
				}
			}
		}
		return resultList;
	}

	@Override
	public List<DayLoad> getDayPowerList(String dateTime, String dateTime2) {
		// DbContextHolder.setDbType("qx");
		// return dayPowerMapper.getDayPowerList(dateTime, dateTime2);
		List<max_powerload> listData = new ArrayList<max_powerload>();
		List<DayLoad> resultList = new ArrayList<DayLoad>();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		List<String> columns = new ArrayList<String>();
		columns.add("to_char(datechar, 'yyyy-MM-dd HH24:MI:SS') as datechar");
		columns.add("dayload");
		selectRequest.setColumns(columns.toArray(new String[columns.size()]));
		selectRequest.setSort(new String[] { "datechar asc" });
		// 拼接where字段
		StringBuilder strWhere = new StringBuilder();
		int whereCount = 0;
		if (dateTime != null) {
			strWhere.append(String.format("and datechar>='%s'", dateTime));
		}
		if (dateTime2 != null) {
			strWhere.append(String.format("and datechar<='%s'", dateTime2));
		}
		if (whereCount == 0 && !StringUtil.IsNullOrEmpty(strWhere.toString())) {
			strWhere = strWhere.replace(0, 4, "");
		}
		selectRequest.setParam(strWhere.toString());
		try {
			selectParam.put("method", "max_powerload");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			listData = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<max_powerload>>() {
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (listData != null && listData.size() > 0) {
			for (max_powerload fcst : listData) {
				DayLoad powerload = new DayLoad();
				powerload.setDate(fcst.getDatechar());
				powerload.setLoad(fcst.getDayload().floatValue());
				resultList.add(powerload);
			}
		}
		return resultList;
	}

	@Override
	public Date getDayTotalPowerTime() {
		// DbContextHolder.setDbType("qx");
		// return dayPowerMapper.getDayTotalPowerTime();
		Date date = new Date();
		Map<String, String> selectParam = new HashMap<String, String>();
		RequestDataBySelect selectRequest = new RequestDataBySelect();
		String[] columns = new String[] { "to_char(datechar, 'yyyy-MM-dd HH24:MI:SS') as datechar" };
		selectRequest.setColumns(columns);
		selectRequest.setSort(new String[] { "datechar desc" });
		selectRequest.setLimit(1);
		try {
			selectParam.put("method", "max_powerload");
			selectParam.put("sqlType", "select");
			selectParam.put("advCode", DbConfig.advCode);
			selectParam.put("param", JsonUtil.object2Json(selectRequest));
			String message = pc.processRequest(selectParam);
			date = JsonUtil.jsonStr2TypeReference(message, new TypeReference<List<max_powerload>>() {
			}).get(0).getDatechar();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return date;
	}

}
